package com.magicsoft.share.ui.share;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.share.Special;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.share.SpecialPresenter;
import com.magicsoft.share.utils.AppBarStateChangeListener;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.share.ISpecialView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

/**
 * @author lyf
 *         专题界面
 */
public class SpecialActivity extends BaseActivity<ISpecialView,SpecialPresenter> implements ISpecialView {


    @BindView(R.id.backdrop)
    ImageView backdrop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    @BindView(R.id.main_content)
    CoordinatorLayout mainContent;
    @BindView(R.id.tv_article_num)
    TextView tvArticleNum;
    @BindView(R.id.tv_watch_num)
    TextView tvWatchNum;
    @BindView(R.id.layout)
    LinearLayout layout;
    private ArticleListAdapter articleListAdapter;
    private Special special;

    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;

    @Override
    public SpecialPresenter initPresenter() {
        return new SpecialPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_special;
    }

    @Override
    public void initData() {
        special = ((Special) getIntent().getSerializableExtra("special"));

    }

    @Override
    public void initView() {
        tvTitle.setText(special.getSpecial_name());
        tvName.setText(special.getSpecial_name());
        tvArticleNum.setText("文章："+special.getArticle_num()+"");
        tvWatchNum.setText("访问："+special.getVisit_num()+"");
        Glide.with(this).load(special.getSpecial_img())
                .apply(bitmapTransform(new BlurTransformation(19)))
                .into(backdrop);

        appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    //展开状态
                    tvTitle.setVisibility(View.GONE);
                    ibtnLeft.setImageResource(R.drawable.back_white);
                } else if (state == State.COLLAPSED) {
                    //折叠状态
                    tvTitle.setVisibility(View.VISIBLE);
                    ibtnLeft.setImageResource(R.drawable.back);
                } else {
                    ibtnLeft.setImageResource(R.drawable.back_white);
                    //中间状态
                }
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(this));



        recyclerView.useDefaultLoadMore();
        //      recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNum++;
                requestData();
            }
        });


        articleListAdapter = new ArticleListAdapter(R.layout.item_article);
        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArticleList.ListBean item = articleListAdapter.getItem(position);
                Intent intent = new Intent(SpecialActivity.this, ArticleActivity.class);
                intent.putExtra("aticle", item);
                intent.putExtra("articleId",item.getArticleId());
                startActivityForResult(intent, Constant.REFRESH_UI);
            }
        });

        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(this, 10), BaseQuickAdapter.HEADER_VIEW));

        recyclerView.setAdapter(articleListAdapter);
    }

    @Override
    public void requestData() {

        mPresenter.articleList(special.getId(),pageNum,pageSize,null,null,null);
    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void ArticleListSuccess(ArticleList data) {
        tvArticleNum.setText("文章："+data.getTotal()+"");
        if (isRefresh) {
            articleListAdapter.setNewData(data.getList());
        } else {
            articleListAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }

    }

    @Override
    public void ArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }
}

